/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import model.SocialMediaBericht;
import persistence.FilmService;
import persistence.SocialMediaBerichtService;

/**
 *
 * @author Matthy & Jelle
 */
public class SociaalMediaBerichtPanel extends JPanel{
  /**
   * Labels.
   */
  private JLabel filmNaamLabel;
  private JLabel tekstLabel;
  private JLabel datumLabel;
  private JLabel netwerkLabel;

  /**
   * Input fields.
   */
  private JTextField filmNaamField;
  private JTextArea tekstArea;
  private JTextField datumField;
  private JComboBox netwerkSelect;

  /**
   * Buttons.
   */
  private JButton submitButton;

  public SociaalMediaBerichtPanel() {
    super();
    initialiseerComponenten();
    layoutComponenten();
    initialiseerEventHandlers();
  }

  private void initialiseerComponenten() {
    // Filmnaam
    this.filmNaamLabel = new JLabel("Film naam*");
    this.filmNaamField = new JTextField();

    // Tekst
    this.tekstLabel = new JLabel("Tekst*");
    this.tekstArea = new JTextArea();
    this.tekstArea.setLineWrap(true);

    // Datum
    this.datumLabel = new JLabel("Datum (dd/mm/yyyy)");
    SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
    this.datumField = new JTextField(format.format(new Date()));

    // Netwerk
    this.netwerkLabel = new JLabel("Netwerk");
    String[] items = {"Facebook", "Twitter"};
    this.netwerkSelect = new JComboBox(items);

    // Submit button
    this.submitButton = new JButton("Opslaan");

  }

  private void layoutComponenten() {
    this.setLayout(new GridLayout(5, 2));

    // Filmnaam
    this.add(this.filmNaamLabel);
    this.add(this.filmNaamField);

    // Tekst
    this.add(this.tekstLabel);
    JScrollPane scrollpane = new JScrollPane(this.tekstArea);
    scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    this.add(scrollpane);

    // Datum
    this.add(this.datumLabel);
    this.add(this.datumField);

    // Netwerk
    this.add(this.netwerkLabel);
    this.add(this.netwerkSelect);

    // Submit button
    this.add(this.submitButton);

  }

  private void initialiseerEventHandlers() {
    this.submitButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        Boolean error = false;
        Date date = new Date();
        if (!datumField.getText().isEmpty()) {
          try {
            SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
            date = format.parse(datumField.getText());
            datumLabel.setForeground(Color.BLACK);
          } catch (ParseException ex) {
            error = true;
            datumLabel.setForeground(Color.RED);
          }
        }

        if (tekstArea.getText().isEmpty()) {
          error = true;
          tekstLabel.setForeground(Color.RED);
        }
        else {
          tekstLabel.setForeground(Color.BLACK);
        }

        if (filmNaamField.getText().isEmpty()) {
          error = true;
          filmNaamLabel.setForeground(Color.RED);
          filmNaamLabel.setText("Film naam*");
        }
        else if (new FilmService().getFilmByName(filmNaamField.getText()) == null) {
          error = true;
          filmNaamLabel.setForeground(Color.RED);
          filmNaamLabel.setText("Film naam* (niet gevonden)");
        }
        else {
          filmNaamLabel.setForeground(Color.BLACK);
          filmNaamLabel.setText("Film naam*");
        }
        if (!error) {
          SocialMediaBericht smb = new SocialMediaBericht();
          smb.setFilmNaam(filmNaamField.getText());
          smb.setNetwerk(netwerkSelect.getSelectedItem().toString());
          smb.setTekst(tekstArea.getText());
          smb.setDatum(date);
          new SocialMediaBerichtService().save(smb);
          reset();
        }
      }
    });
  }

  private void reset() {
    this.filmNaamField.setText("");
    this.tekstArea.setText("");
    this.datumField.setText("");
    this.netwerkSelect.setSelectedIndex(0);
  }

}
